package com.gmono.managers;

import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;

/**
 * 用户管理器
 */
public class ArticleManager {
    static public Connection c=Common.c;


    static public Article GenerateArticle(ResultSet row)
    {
        Article u=new Article();
        try {
            u.setId(row.getInt("id"));
            u.setTitle(row.getString("title"));
            u.setContent(row.getString("content"));
            u.setDescribe(row.getString("describe"));
            u.setTime(row.getTimestamp("time"));
            u.setCategory(row.getInt("category"));
            u.setUserid(row.getInt("userid"));
            u.setVisibility(row.getBoolean("visibility"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return u;
    }
    static public Article getArticle(int id)
    {
        try {
            PreparedStatement s=c.prepareStatement("SELECT * from article where id= ? ");
            s.setInt(1,id);
            ResultSet set=s.executeQuery();
            set.next();
            return GenerateArticle(set);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

    }
    public static boolean deleteArticle(int id)
    {
        try
        {
            PreparedStatement s=c.prepareStatement("delete FROM article where id= ? ");
            s.setInt(1,id);
            s.executeUpdate();
            return true;
        }catch(SQLException e)
        {
            e.printStackTrace();
            return false;
        }
    }
    public static boolean updateArticle(int id,Article article)
    {
        try
        {
            PreparedStatement s=c.prepareStatement("UPDATE  article SET title=?,content=?,describe=?,time=?,visibility=?,category=? where id= ? ");
            s.setString(1,article.getTitle());
            s.setString(2,article.getContent());
            s.setString(3,article.getDescribe());
            s.setTimestamp(4,article.getTime());
            s.setBoolean(5,article.isVisibility());
            s.setInt(6,article.getCategory());
            s.setInt(7,id);
            s.executeUpdate();
            return true;
        }catch(SQLException e)
        {
            e.printStackTrace();
            return false;
        }
    }
    public static boolean addArticle(Article article)
    {
        try
        {
            PreparedStatement s=c.prepareStatement("INSERT INTO article(title,content,describe,time,visibility,category,userid) VALUES (?,?,?,?,?,?,?);");
            s.setString(1,article.getTitle());
            s.setString(2,article.getContent());
            s.setString(3,article.getDescribe());
            s.setTimestamp(4,article.getTime());
            s.setBoolean(5,article.isVisibility());
            s.setInt(6,article.getCategory());
            s.setInt(7,article.getUserid());
            s.executeUpdate();
            return true;
        }catch(SQLException e)
        {
            e.printStackTrace();
            return false;
        }
    }


    static public Collection<Article> getArticles()
    {
        try {
            PreparedStatement s=c.prepareStatement("select * from article ORDER BY time");
            ResultSet set=s.executeQuery();
            ArrayList<Article> lst=Common.ResultToCollection(set,ArticleManager::GenerateArticle);
            return lst;
        } catch (SQLException e) {
            return null;
        }

    }

}
